package com.worldtvs.online.view;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bzydroid.network.http.IRequestHandler;
import com.bzydroid.network.http.NetworkResponse;
import com.bzydroid.network.http.client.BzydroidHttpClient;
import com.bzydroid.network.http.exception.BzydroidNetworkExeception;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnFullscreenListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import com.worldtv.lives.R;
import com.worldtvs.online.E4KidsConfig;
import com.worldtvs.online.YouTubeFailureRecoveryActivity;
import com.worldtvs.online.service.RequestService;
import com.worldtvs.online.util.Debug;

public class PlayVideoYoutbe extends YouTubeFailureRecoveryActivity implements
		OnFullscreenListener, OnClickListener {

	private boolean fullscreen;
	private YouTubePlayerView youTubeView;
	private RelativeLayout rlAds;
	private LinearLayout baseLayout;
	private ImageView ivCancel;
	private AdView adView;
	private InterstitialAd interstitial;
	private String linkVideo;
	private String idVideo;

	private RelativeLayout rlContainAds;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.layout_detail_video_custom);

		Bundle bundle = getIntent().getExtras();

		if (bundle != null) {
			linkVideo = bundle.getString(E4KidsConfig.KEY_INTENT_LINK);
			idVideo = bundle.getString(E4KidsConfig.KEY_INTENT_VID);

			Debug.debug("Link Video : " + linkVideo);
		}

		RequestService.rqTangView(idVideo, new IRequestHandler() {

			@Override
			public void onWait(BzydroidHttpClient arg0) {
			}

			@Override
			public void onSuccess(BzydroidHttpClient arg0, NetworkResponse arg1) {
				Debug.debug("Tang View thanh cong : "
						+ arg0.getResponse().toString());
			}

			@Override
			public void onStarted(BzydroidHttpClient arg0) {
			}

			@Override
			public void onFinished(BzydroidHttpClient arg0) {
			}

			@Override
			public void onError(BzydroidHttpClient arg0,
					BzydroidNetworkExeception arg1) {
			}
		});

		rlContainAds = (RelativeLayout)findViewById(R.id.rlContainAds);
		interstitial = new InterstitialAd(getBaseContext());
		interstitial.setAdUnitId(getString(R.string.intshow));
		 adView = (AdView) this.findViewById(R.id.ad);
		AdRequest adRequest = new AdRequest.Builder()
		 .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
		 .addTestDevice("")
				.build();
		adView.loadAd(adRequest);
		interstitial.loadAd(adRequest);
		interstitial.setAdListener(new AdListener() {
			@Override
			public void onAdLoaded() {
				 displayInterstitial();
			}
		});

		baseLayout = (LinearLayout) findViewById(R.id.layout);

		youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
		youTubeView.initialize(E4KidsConfig.API_YOUTUBE, this);

	}
	public void displayInterstitial() {
		if (interstitial.isLoaded()) {
			interstitial.show();
		}
	}
	@Override
	public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
			boolean arg2) {
		if (!arg2) {

			arg1.setOnFullscreenListener(this);
			arg1.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);

			// arg1.loadPlaylist("PL9C2D08CA03BB249A");
			arg1.loadVideo(linkVideo);

			arg1.setPlayerStyle(PlayerStyle.DEFAULT);

			// arg1.

		}
	}

	@Override
	protected Provider getYouTubePlayerProvider() {
		return (YouTubePlayerView) findViewById(R.id.youtube_view);
	}

	@Override
	public void onFullscreen(boolean arg0) {
		if (arg0) {
			rlContainAds.setVisibility(View.GONE);
		} else {
			rlContainAds.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onClick(View v) {

	}

}
